package com.basic.modules.ykf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.basic.common.utils.*;
import com.basic.modules.sys.entity.SysUserEntity;
import com.basic.modules.sys.model.MainPageShopDetailModel;
import com.basic.modules.sys.model.MainPageShopModel;
import com.basic.modules.ykf.entity.YkfCustomerShopHistoryEntity;
import com.basic.modules.ykf.entity.YkfCustomerSubAccountEntity;
import com.basic.modules.ykf.entity.YkfShopChangeRecordEntity;
import com.basic.modules.ykf.service.YkfCustomerShopHistoryService;
import com.basic.modules.ykf.service.YkfCustomerSubAccountService;
import com.basic.modules.ykf.service.YkfShopChangeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.basic.modules.ykf.dao.YkfCustomerShopDao;
import com.basic.modules.ykf.entity.YkfCustomerShopEntity;
import com.basic.modules.ykf.service.YkfCustomerShopService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service("ykfCustomerShopService")
public class YkfCustomerShopServiceImpl extends ServiceImpl<YkfCustomerShopDao, YkfCustomerShopEntity> implements YkfCustomerShopService {

    @Lazy
    @Autowired
    private YkfCustomerSubAccountService customerSubAccountService;

    @Lazy
    @Autowired
    private YkfCustomerShopHistoryService shopHistoryService;

    @Lazy
    @Autowired
    private YkfShopChangeRecordService changeRecordService;

    @Override
    public PageUtils queryPage(ParamsModel<YkfCustomerShopEntity> params) {
        QueryWrapper<YkfCustomerShopEntity> queryWrapper = ParamUtils.createQueryWrapperByDefaultSort(params);
        if (ObjectUtil.isNotEmpty(params.getCondition().getKeyword())) {
            queryWrapper.lambda().like(YkfCustomerShopEntity::getShopName, params.getCondition().getKeyword());
        }
        IPage<YkfCustomerShopEntity> page = this.page(
                new Query<YkfCustomerShopEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<MainPageShopModel> assignAlert() {
        return baseMapper.assignAlert();
    }

    @Override
    public List<MainPageShopDetailModel> assignAlertDetail(String shopId) {
        List<MainPageShopDetailModel> resList = new ArrayList<>();
        QueryWrapper<YkfCustomerSubAccountEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(YkfCustomerSubAccountEntity::getShopId, shopId);
        List<YkfCustomerSubAccountEntity> list = customerSubAccountService.list(queryWrapper);
        if (ObjectUtil.isNotEmpty(list)) {
            for (YkfCustomerSubAccountEntity item : list) {
                MainPageShopDetailModel res = new MainPageShopDetailModel();
                BeanUtil.copyProperties(item, res);
                resList.add(res);
            }
        }
        return resList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void addShop(YkfCustomerShopEntity ykfCustomerShop) {
        //查询店铺编号
        QueryWrapper<YkfCustomerShopEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(YkfCustomerShopEntity::getCustomerId, ykfCustomerShop.getCustomerId());
        int count = this.count(queryWrapper);
        ykfCustomerShop.setShopNo("SHOP" + String.format("%02d", count + 1));
        SysUserEntity user = CommonUtils.getUser();
        if (ykfCustomerShop.getCooperateStatus() == 0) {
            ykfCustomerShop.setStopCooperateDate(DateUtil.date());
            ykfCustomerShop.setStopUser(user.getName());
            ykfCustomerShop.setStopUserDept(user.getDeptName());
        }
        if (ObjectUtil.isEmpty(ykfCustomerShop.getEndDate())) {
//            ykfCustomerShop.setEndDate(DateUtil.offset(ykfCustomerShop.getStartDate(), DateField.YEAR, 100));
            ykfCustomerShop.setEndDate(DateUtil.parseDate("2188-01-01"));
        }
        this.save(ykfCustomerShop);

        YkfCustomerShopHistoryEntity history = new YkfCustomerShopHistoryEntity();
        BeanUtil.copyProperties(ykfCustomerShop, history);
        history.setOperator(user.getName());
        shopHistoryService.save(history);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updateCustomerShop(YkfCustomerShopEntity ykfCustomerShop) {
        SysUserEntity user = CommonUtils.getUser();
        if (ykfCustomerShop.getCooperateStatus() == 0) {
            ykfCustomerShop.setStopCooperateDate(DateUtil.date());
            ykfCustomerShop.setStopUser(user.getName());
            ykfCustomerShop.setStopUserDept(user.getDeptName());
        }
        if (ykfCustomerShop.getFeeModel() == 1) {
            ykfCustomerShop.setMergeGroup(null);
        }
        if (ObjectUtil.isEmpty(ykfCustomerShop.getEndDate())) {
//            ykfCustomerShop.setEndDate(DateUtil.offset(ykfCustomerShop.getStartDate(), DateField.YEAR, 100));
            ykfCustomerShop.setEndDate(DateUtil.parseDate("2188-01-01"));
        }
        this.updateById(ykfCustomerShop);
        //保存修改记录
        YkfShopChangeRecordEntity record = new YkfShopChangeRecordEntity();
        BeanUtil.copyProperties(ykfCustomerShop, record);
        record.setUpdTime(DateUtil.date());
        record.setUpdUserId(user.getUserId());
        record.setUpdUserName(user.getName());
        changeRecordService.save(record);

        YkfCustomerShopHistoryEntity history = new YkfCustomerShopHistoryEntity();
        BeanUtil.copyProperties(ykfCustomerShop, history);
        history.setOperator(user.getName());
        shopHistoryService.save(history);
    }

}
